package com.dchh.android.suverynew.base;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;

import com.dchh.android.suverynew.R;
import com.dchh.android.suverynew.common.SPreferencesManager;
import com.dchh.android.suverynew.view.ICommonView;
import com.dhcc.android.base.base.BaseActivity;
import com.dhcc.android.base.base.MSApplication;
import com.dhcc.android.base.common.view.MyLoadingDialog;
import com.dhcc.android.base.utils.CommenUtil;
import com.dhcc.android.base.utils.Constants;
import com.dhcc.android.base.utils.Msg;
import com.dhcc.android.base.utils.version.APKDownload;

import common.eric.com.ebaselibrary.util.ToastUtils;

/**
 * Created by eric on 2016/8/2.
 */
public abstract class SBaseActivity extends BaseActivity implements ICommonView {
    private Dialog mLoadingDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MSApplication.instance().addActivity(this);
        SPreferencesManager.initContext(this);
    }

    @Override
    public void showProgressDialog() {
        showProgressDialog("");
    }

    @Override
    public void showProgressDialog(String msg) {
        if (mLoadingDialog == null)
            mLoadingDialog = MyLoadingDialog.createLoadingDialog(this, msg);
        mLoadingDialog.show();
    }

    @Override
    public void closeProgressDialog() {
        mLoadingDialog.dismiss();
    }

    protected void alertInfo(String msg) {
        Msg.showInfo(this, msg);
    }

    @Override
    public void onError(String errMsg) {
        alertInfo(errMsg);
    }

    @Override
    public String getVersionName() throws PackageManager.NameNotFoundException {
        //getPackageName()是你当前类的包名，0代表是获取版本信息
        PackageManager packageManager = getPackageManager();
        PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(), 0);
        return packInfo.versionName;
    }


    /**
     * 版本更新弹窗
     */
    protected void showUpdataDialog() {
        AlertDialog.Builder builer = new AlertDialog.Builder(this);
        builer.setTitle("版本升级");
        builer.setMessage("检测到最新版本，请及时更新！");
        //当点确定按钮时从服务器上下载 新的apk 然后安装   װ
        builer.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                if (Constants.isNetworkAvailable(SBaseActivity.this)) {
                    new APKDownload(SBaseActivity.this).downLoadApk();

                } else {
                    CommenUtil.toast("网络不可用，无法下载更新", SBaseActivity.this);
                }
            }
        });
        builer.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        android.app.AlertDialog dialog = builer.create();
        dialog.show();
    }

    @Override
    protected void setToolBarNavigationIcon(Toolbar toolbar) {
        toolbar.setNavigationIcon(R.mipmap.arrow_left_white_large);
    }

    @Override
    public void toast(String msg) {
        ToastUtils.show(this, msg);
    }

    @Override
    public boolean isNetWorkAvailable() {
        return Constants.isNetworkAvailable(this);
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        new MSApplication().deleteActivity(this);
        super.onDestroy();
    }

}
